package com.huishu.tcm.util;

import java.io.File;
import java.sql.*;
import java.util.*;
import java.util.stream.Collectors;

public class Main {
    // 生成type列表
    public static void main1(String[] args) throws ClassNotFoundException, SQLException {
        Class.forName("com.mysql.cj.jdbc.Driver");
        Connection connection = DriverManager.getConnection("jdbc:mysql://47.110.35.18:7777/tinting_system?useUnicode=true&characterEncoding=utf8&useSSL=false&allowPublicKeyRetrieval=true&autoReconnect=true",
            "root","qazplm.12");
        String path = "/Users/zhangmingli/Documents/汇数数科/软著软件/东唐贴图系统素材";
        File file = new File(path);
        String [] a = file.list();
        List<String> list = Arrays.asList(a);
        List<String> dirList = list.stream().filter(item -> !item.startsWith(".")).collect(Collectors.toList());
        StringBuilder sql = new StringBuilder("insert into `t_mapping_system_image_type` (`type`) values ");
        for(String type: dirList){
            sql.append("('");
            sql.append(type);
            sql.append("'),");
        }
        sql.delete(sql.length() - 1,sql.length());
        PreparedStatement preparedStatement = connection.prepareStatement(sql.toString());
        preparedStatement.execute();
        preparedStatement.close();
        connection.close();
    }

    // 生成文件列表
    public static void main(String[] args) throws ClassNotFoundException, SQLException {
        String urlPre = "http://123.56.218.205:8002/images/";
        Class.forName("com.mysql.cj.jdbc.Driver");
        Connection connection = DriverManager.getConnection("jdbc:mysql://47.110.35.18:7777/tinting_system?useUnicode=true&characterEncoding=utf8&useSSL=false&allowPublicKeyRetrieval=true&autoReconnect=true",
            "root","qazplm.12");
        String pathPre = "/Users/zhangmingli/Documents/汇数数科/软著软件/东唐贴图系统素材/";
        File dirList = new File(pathPre);
        String [] a = dirList.list();
        // 查询出分类列表
        String sql = "select `id`,`type` from `t_mapping_system_image_type`";
        PreparedStatement preparedStatement = connection.prepareStatement(sql);
        ResultSet rs =preparedStatement.executeQuery(sql);
        List<Map<String,Object>> typeList = new ArrayList<>();
        while (rs.next()){
            Map<String, Object> map = new HashMap<>();
            String type = rs.getString("type");
            int id = rs.getInt("id");
            map.put("id",id);
            map.put("type",type);
            typeList.add(map);
        }
        rs.close();
        preparedStatement.close();
        List<String> list = Arrays.asList(a).stream().filter(item -> !item.startsWith(".")).collect(Collectors.toList());
        StringBuilder insertSql = new StringBuilder("insert into `t_mapping_system_image` (`url`,`remark`,`type_id`) values ");
        for(String dir: list){
            int typeId = 0;
            for(Map<String,Object> map: typeList){
                if(map.get("type").equals(dir)){
                    typeId = (Integer) map.get("id");
                    break;
                }
            }
            String dirPath = pathPre + dir + "/";
            File fileList = new File(dirPath);
            String[] b = fileList.list();
            List<String> fileNameList = Arrays.asList(b).stream().filter(item -> !item.startsWith(".")).collect(Collectors.toList());
            for(String fileName : fileNameList){
                if(fileName.startsWith(".")){
                    continue;
                }
                String url = urlPre + dir + "/" + fileName;
                insertSql.append("('");
                insertSql.append(url);
                insertSql.append("','");
                insertSql.append(fileName.split("\\.")[0]);
                insertSql.append("',");
                insertSql.append(typeId);
                insertSql.append("),");
            }
        }
        preparedStatement = connection.prepareStatement(insertSql.delete(insertSql.length() - 1,insertSql.length()).toString());
        preparedStatement.execute();
        preparedStatement.close();
        connection.close();
    }
}
